<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class News extends Model
{
    protected $table = 'news';
    public $timestamps = false;
    protected $guarded = [
        'id', 'backgroundImage', 'invite_time', 'article', 'title', 'create_at', 'updated_at'
    ];

    public function getData($search = [], $page = 1, $length = 20){
        $ExamplesList = array();
        $ExamplesList['pageData'] = array();
        $date = date('Y-m-d H:i:s', time());

        $sql = \DB::table($this->table)
            ->whereNull('deleted_at');

        if(isset($search['title'])){
            $sql->where('title', 'like', '%'.$search['title'].'%');
        }
        if(isset($search['id'])){
            $sql->where('id', 'like', '%'.$search['id'].'%');
        }

        $ExamplesList['pageData']['count']     = $sql->count();
        $ExamplesList['pageData']['lastPage']  = ceil($ExamplesList['pageData']['count'] / $length);

        $ExamplesList['list'] = $sql->orderBy('invite_time', 'desc')
            ->skip(($page-1) * $length)->take($length)
            ->get();

        return $ExamplesList;
    }

    public function saveData($data){
        return \DB::table($this->table)->insert($data);
    }


    public function setData($id,$data){
        return \DB::table($this->table)->where('id',$id)->update($data);
    }


    public function delData($id,$data){
        return \DB::table($this->table)->where('id',$id)->update($data);
    }
}